﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data.GameData.Utils
{
    public static class GameDataStore
    {
        private static Dictionary<string, List<GameObject>> dataStore = new Dictionary<string, List<GameObject>>();

        /// <summary>
        /// Add a new object to the data store.
        /// </summary>
        /// <param name="storeId">The data store to add to</param>
        /// <param name="obj">The object to place in the store</param>
        public static void Add(string storeId, GameObject obj)
        {
            List<GameObject> objects = null;

            // If this is a new data store then create a new store.
            if (!dataStore.ContainsKey(storeId))
                dataStore.Add(storeId, new List<GameObject>());

            objects = dataStore[storeId];

            if (!objects.Contains(obj))
                objects.Add(obj);
        }

        /// <summary>
        /// Remove an item from a data store.
        /// </summary>
        /// <param name="storeId">The data store to remore from</param>
        /// <param name="obj">The object to place in the store</param>
        public static void Remove(string storeId, GameObject obj)
        {
            if (!dataStore.ContainsKey(storeId))
                return;

            dataStore[storeId].Remove(obj);
        }

        /// <summary>
        /// Retrieve a specific data store
        /// </summary>
        /// <param name="mapId">The data store to retrieve</param>
        /// <returns>The list of game objects in the store</returns>
        public static List<GameObject> GetStoreContents(string storeId)
        {
            if (dataStore.ContainsKey(storeId))
                return dataStore[storeId];
            else
                return new List<GameObject>();
        }

        /// <summary>
        /// Retrieve a specific ship class
        /// </summary>
        /// <param name="shipClass">The ship class code to retrieve</param>
        /// <returns>The ship class</returns>
        public static ShipClass GetShipClass(string shipClass)
        {
            if (dataStore.ContainsKey("ShipClass"))
                return dataStore["ShipClass"].Find(ship => ship.ToString() == shipClass) as ShipClass;

            return null;
        }

        /// <summary>
        /// Retrieve a specific ship system
        /// </summary>
        /// <param name="shipSystem">The ship system code to retrieve</param>
        /// <returns>The ship system</returns>
        public static ShipSystem GetShipSystem(string shipSystem)
        {
            foreach (KeyValuePair<string, List<GameObject>> store in dataStore)
            {
                if (store.Value[0] == null || !(store.Value[0] is ShipSystem))
                    continue;

                ShipSystem sys = store.Value.Find(system => system.ToString() == shipSystem) as ShipSystem;
                if (sys != null && sys.Code == shipSystem)
                    return sys;
            }

            return null;
        }

        /// <summary>
        /// Empty an entire store
        /// </summary>
        /// <param name="store">The store to empty</param>
        public static void EmptyStore(string store)
        {
            if (dataStore.ContainsKey(store))
                dataStore[store] = new List<GameObject>();
        }

        /// <summary>
        /// >Retrieve a unique list of Ship Systems (and Weapons)
        /// </summary>
        /// <returns>The list if ship systems</returns>
        public static List<string> GetShipSystemNames()
        {
            List<string> systemList = new List<string>();

            foreach (KeyValuePair<string, List<GameObject>> store in dataStore)
            {
                if (store.Value[0] == null || !(store.Value[0] is ShipSystem))
                    continue;

                foreach (ShipSystem sys in store.Value)
                {
                    if (!systemList.Contains(sys.Name))
                        systemList.Add(sys.Name);
                }
            }

            return systemList;
        }

        /// <summary>
        /// Retrieve all the generations of the same ship system ordered by SL level
        /// </summary>
        /// <param name="shipSystemName">The ship system to retrieve</param>
        /// <returns>The ordered list of ship system generations</returns>
        public static List<ShipSystem> GetShipSystemGenerations(string shipSystemName)
        {
            List<ShipSystem> systemList = new List<ShipSystem>();

            foreach (KeyValuePair<string, List<GameObject>> store in dataStore)
            {
                if (store.Value[0] == null || !(store.Value[0] is ShipSystem))
                    continue;

                foreach (ShipSystem sys in store.Value)
                {
                    if (sys.Name == shipSystemName)
                        systemList.Add(sys);
                }
            }

            return systemList.OrderBy(sys => sys.SL).ToList();
        }

        /// <summary>
        /// Retrieve all the ship classes
        /// </summary>
        /// <returns>The ship classes</returns>
        public static List<ShipClass> GetShipClasses()
        {
            return dataStore["ShipClass"].Cast<ShipClass>().ToList();
            //List<ShipClass> classList = new List<ShipClass>();

            //foreach (ShipClass sClass in dataStore["ShipClass"])
            //    classList.Add(sClass);

            //return classList;
        }
    }
}
